﻿using System.ComponentModel;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using TomNet.App.Core.Contracts;
using TomNet.App.Model.DB;
using TomNet.App.Model.DTO;
using TomNet.AspNetCore.Data;
using TomNet.SqlSugarCore.Entity;
using TomNet.App.Model.DB.Identity;
using System.Security.Claims;
using TomNet.Security.Claims;
namespace TomNet.App.Web.Controllers
{
    [Authorize]
    [Description("我的奖励订单")]
    public class MyRewardBusinessController : Controller
    {
        private readonly IUserRewardBusinessContract _userRewardBusinessContract;
        private readonly IUnitOfWork _unitOfWork;
        public MyRewardBusinessController(IUserRewardBusinessContract userRewardBusinessContract,
            IUnitOfWork unitOfWork)
        {
            _userRewardBusinessContract = userRewardBusinessContract;
            _unitOfWork = unitOfWork;
        }
        [Description("我的奖励订单列表")]
        [HttpGet]
        public IActionResult MyRewardBusinessList()
        {
            return View();
        }
        [Description("我的奖励订单列表")]
        [HttpPost]
        public IActionResult MyRewardBusinessList(int page = 1, int limit = 20)
        {
            var userId = HttpContext.User.Identity.GetClaimValueFirstOrDefault(ClaimTypes.NameIdentifier);
            var query = _userRewardBusinessContract.UnitOfWork.DbContext.Queryable<UserRewardBusiness, Business, Users>((ub, b, u)
                   => ub.UserID == u.Id && ub.BusinessID == b.Id)
                .Where((ub, b, u) => ub.UserID.ToString() == userId)
                .OrderBy((ub, b, u) => ub.Id, OrderByType.Desc)
               .Select((ub, b, u) => new UserRewardBusinessDto
               {
                   UserName = u.UserName,
                   UserID = u.Id,
                   Hashrate = ub.Hashrate,
                   Kwh = ub.Kwh,
                   BeginTime = ub.BeginTime,
                   EndTime = ub.EndTime,
                   CreatedTime = ub.CreatedTime,
                   Remark = ub.Remark
               });
            int total = 0;
            var list = query.ToPageList(page, limit, ref total);
            var result = new LayuiPageResult<UserRewardBusinessDto>
            {
                Code = 0,
                Msg = "Success",
                Count = total,
                Data = list.ToArray()
            };
            return Json(result);
        }
        
    }

}